package mbms.mobilebudget.creditCard;

import java.sql.SQLException;
import java.util.List;

import mbms.mobilebudget.database.DatabaseHandler;
import mbms.mobilebudget.entity.CreditCard;
import mbms.mobilebudget.entity.Expense;
import mbms.mobilebudget.entity.Income;
import mbms.mobilebudget.entity.User;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.j256.ormlite.dao.Dao;

@SuppressLint("ShowToast")
public class CreditCardManager {

	private DatabaseHandler helper;
	private Context context;

	public CreditCardManager(DatabaseHandler helper, Context context) {
		this.helper = helper;
		this.context = context;
	}
	
	public CreditCard addCreditCard(CreditCard card, User authenticatedUser) {
		try {
			card.setUser(authenticatedUser);
			card.setTotalIncomeAmount(0.0);
			card.setTotalExpenseAmount(0.0);
			Dao<CreditCard,Integer> creditCardDao = helper.getCreditCardDao();
			creditCardDao.create(card);
			Log.d("Insertion",creditCardDao.queryForId(card.getId()).getId()+ " id inserted");

			return card;
			
		} catch (SQLException e) {
			Log.e("error", "add credit card");
		}
		
		return null;
	}
	public CreditCard updateCreditCard(CreditCard card) {
		try {	
			Dao<CreditCard,Integer> creditCardDao = helper.getCreditCardDao();
			CreditCard updatedCard= creditCardDao.queryForId(card.getId());
			if(updatedCard==null || updatedCard.getId()==null){
				Toast.makeText(context, "id not retrieved while updating", Toast.LENGTH_LONG);
		}
		else{
			copyCreditCard(card, updatedCard);
			creditCardDao.update(updatedCard);
				Toast.makeText(context, "update with id : " + updatedCard.getId(),
						Toast.LENGTH_LONG);
			return updatedCard;
		}
			
		} catch (SQLException e) {
			Log.e("error", "update credit card");
		}
		
		return null;
	}
	private void copyCreditCard(CreditCard gives, CreditCard takes){
		takes.setBankName(gives.getBankName());
		takes.setBranch(gives.getBranch());
		takes.setCurrencyType(gives.getCurrencyType());
		takes.setLastFourDigit(gives.getLastFourDigit());

		
	}
	public CreditCard deleteCreditCard(CreditCard account){
		try {
			Dao<CreditCard,Integer> creditCardDao = helper.getCreditCardDao();
			CreditCard toBeDeleted= creditCardDao.queryForId(account.getId());
			
			if(toBeDeleted==null || toBeDeleted.getId()==null){
				Toast.makeText(context, "id not retrieved while deleting", Toast.LENGTH_LONG);
			}
			else{
				creditCardDao.delete(toBeDeleted); Toast.makeText(context, "deleted with id : " + toBeDeleted.getId(),
							Toast.LENGTH_LONG);
				return toBeDeleted;
			}
		} catch (SQLException e) {
			Log.e("error", "update credit card");
		}
		return null;
	}
	
	public List<Income> getIncomeListOfCreditCard(CreditCard creditCard) {
		if(creditCard == null || creditCard.getId()==null){
			Toast.makeText(context, "list failed",
					Toast.LENGTH_LONG);
			return null;
		}
		try {
			Dao<Income, Integer> incomeDao = helper.getIncomeDao();
			List <Income> incomes = incomeDao.queryForEq("creditCard_id", creditCard.getId());
			return incomes;
			
		} catch (SQLException e) {
			Log.e("list", "error");
			e.printStackTrace();
		}
		
		return null;
	}
	
	
	public List<Expense> getExpenseListOfCreditCard(CreditCard creditCard) {
		if(creditCard == null || creditCard.getId()==null){
			Toast.makeText(context, "list failed",
					Toast.LENGTH_LONG);
			return null;
		}
		try {
			Dao<Expense, Integer> expenseDao = helper.getExpenseDao();
			List <Expense> expenses = expenseDao.queryForEq("creditCard_id", creditCard.getId());
			if(expenses!=null){
				for (Expense exp : expenses) {
					Log.d("List", exp.getId() + " exists with amout "+
							exp.getAmount());
				}
				return expenses;
			}
			
		} catch (SQLException e) {
			Log.e("list", "error");
			e.printStackTrace();
		}
		
		return null;
		
	}
}
